package com.zhangtai.modules.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.zhangtai.common.annotation.SysAuthorityLog;
import com.zhangtai.exception.QwMsg;
import com.zhangtai.modules.constVo.NoticeConst;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.entity.employee.EmployeeChangeEntity;
import com.zhangtai.modules.service.employee.EmployeeChangeService;
import com.zhangtai.modules.vo.EmployeeChangeVo;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import com.zhangtai.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(tags="员工信息变动")
@RestController
@RequestMapping("employeeChange")
@Slf4j
public class EmployeeChangeController {

    @Autowired
    private QwMsg qwMsg;

    @Autowired
    private EmployeeChangeService employeeChangeService;

    @Autowired
    private LoginController loginController;

    public R addEmployeeChangeInfo(@RequestBody @Valid EmployeeChangeEntity entity){
        String content = "您有一条待确认的员工信息变更："+entity.getName();
//        JSONObject userInfo  = loginController.getUserInfo(request).getData();
//        Long operationId = userInfo.getLong("id");
        qwMsg.sendNoticeMsg("员工信息变更",content, NoticeConst.TODO_TYPE_EMPLOYEE_CHANGE,false,entity.getOid(),null,"账号管理员",null,new Date(),entity.getUnitId());
        employeeChangeService.saveE(entity);
        return R.ok();
    }

    @PostMapping("/getList")
    @ApiOperation("员工信息变更列表")
    public R<PageResult<EmployeeChangeEntity>> getList(@RequestBody @Valid EmployeeChangeVo vo,HttpServletRequest request)throws  Exception{
        //获取当前用户角色下公司Id
        R checkR = loginController.checkOrgPermission(request,null,false);
        if(!R.isOk.equals(checkR.getStatus())){
            return checkR;
        }
        List<Long> orgIds = (List<Long>) checkR.getData();
        PageResult<EmployeeChangeEntity> entityPageResult = employeeChangeService.getList(vo.getName(),vo.getType(),vo.getStatus(),orgIds,vo.getPageNum(),vo.getPageSize());
        return R.ok(entityPageResult);
    }

    @PostMapping("/confirmChange")
    @ApiOperation("变更确认")
    @SysAuthorityLog(value = "变更确认")
    @Transactional
    public R confirmChange(String id,HttpServletRequest request)throws Exception{
        if(StringUtils.isEmpty(id)){
            return R.error("变更id不存在！");
        }
        EmployeeChangeEntity changeEntity = employeeChangeService.getByIdE(Long.parseLong(id));
        JSONObject userInfo = loginController.getUserInfo(request).getData();
        changeEntity.setStatus(1);
        changeEntity.setConfirmCode(userInfo.getString("usercode"));
        changeEntity.setCreateTime(new Date());
        employeeChangeService.updateByIdE(changeEntity);
        return R.ok();
    }
}
